﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;
using System.Windows;

namespace Email_Validator
{
    public class exchangeauthinfo
    {

        private string _userName;

        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        private string _passWord;

        public string Password
        {
            get { return _passWord; }
            set { _passWord = value; }
        }

        private string _ewsUrl;

        public string EwsUrl
        {
            get { return _ewsUrl; }
            set { _ewsUrl = value; }
        }

        private ExchangeService _exchservice = null;

        public ExchangeService EwsService
        {
            get { return _exchservice; }
            set { _exchservice = value; }
        }

        private bool _impersonate;

        public bool Impersonate
        {
            get { return _impersonate; }
            set { _impersonate = value; }
        }

        private string _smtpAddress;
        public string SmtpAddress
        {
            get { return _smtpAddress; }
            set { _smtpAddress = value; }
        }

        private bool _exchangeOnline;
        public bool ExchangeOnline
        {
            get { return _exchangeOnline; }
            set { _exchangeOnline = value; }
        }

        private bool _defaultCredentials;
        public bool DefaultCredentials
        {
            get { return _defaultCredentials; }
            set { _defaultCredentials = value; }
        }

        private string _domain;

        public string Domain
        {
            get { return _domain; }
            set { _domain = value; }
        }

        private ExchangeVersion _version;

        public ExchangeVersion Version
        {
            get { return _version; }
            set { _version = value; }
        }

        public bool IsServiceReady
        {
            get { return _isServiceReady; }
            set { _isServiceReady = value; }
        }

        private bool _isServiceReady = false;

        public PropertySet FindItemsProps; // = new PropertySet(ItemSchema.DateTimeReceived, ItemSchema.Subject, ItemSchema.Id);
        
      

        public exchangeauthinfo(string username, string password, bool impersonate, string smtpaddress, string ewsurl, bool exchangeonline, string domain, string version)
        {
            UserName = username;
            Password = password;
            Impersonate = impersonate;
            SmtpAddress = smtpaddress;
            EwsUrl = ewsurl;
            ExchangeOnline = exchangeonline;
            Domain = domain;
            FindItemsProps = SetItemProperties();

            if (version == "Exchange 2016" || String.IsNullOrEmpty(version))
            {
                Version = ExchangeVersion.Exchange2013_SP1;
            }

            if (version == "Exchange 2013")
            {
                Version = ExchangeVersion.Exchange2013;
            }

            if (version == "Exchange 2010")
            {
                Version = ExchangeVersion.Exchange2010_SP2;
            }

        }

        private PropertySet SetItemProperties()
        {
            ExtendedPropertyDefinition crmid = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings,"crmid",MapiPropertyType.String);
            ExtendedPropertyDefinition crmlinkstate = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings,"crmLinkState",MapiPropertyType.Double);

            PropertySet properties = new PropertySet(ItemSchema.DateTimeReceived, ItemSchema.Subject, crmlinkstate, crmid, ItemSchema.Id);
            return properties;
        }

        internal void Run()
        {
            BuildService();
            TestAccess();
        }

        private void BuildService()
        {
            EwsService = new ExchangeService(Version);
            if (DefaultCredentials)
            {
                EwsService.UseDefaultCredentials = true;
            }
            else if (ExchangeOnline)
            {
                EwsService.Credentials = new WebCredentials(UserName, Password);
            }
            else
            {
                EwsService.Credentials = new WebCredentials(UserName, Password, Domain);
            }
            EwsService.Url = new Uri(EwsUrl);

            if (Impersonate)
            {
                EwsService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, SmtpAddress);
            }
        }

        private void TestAccess()
        {

            try {
                if (Impersonate)
                {
                    Folder BoundFolderImpersonation = Folder.Bind(EwsService, new FolderId(WellKnownFolderName.Inbox, SmtpAddress));
                }
                else
                {
                     Folder BoundFolder = Folder.Bind(EwsService, new FolderId(WellKnownFolderName.Inbox));
                }
                IsServiceReady = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(MainWindow.GetErrorDetail(ex), "Exception during Exchange Service Acquisition");
                IsServiceReady = false;
            }
        }
    }
}
